layui.use(['layer', 'carousel','form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var carousel = layui.carousel;
    var device = layui.device;
    var form = layui.form;

    /**
     * 系统管理--用户管理
     */
    var MgrUser = {
        tableId: "userTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    // 渲染轮播
    carousel.render({
        elem: '.layui-carousel',
        width: '100%',
        height: '60px',
        arrow: 'none',
        autoplay: true,
        trigger: device.ios || device.android ? 'click' : 'hover',
        anim: 'fade'
    });

    $(function () {

        /*点击日程框架栏的更多*/
        $("#sharedSform").click(function () {
            Feng.success("正在跳转到所有已发布的日程框架页面");
            window.location.href="/shareCommunity/showShareSform";
        });

        /*点击日程表栏的更多*/
        $("#sharedSframe").click(function () {
            Feng.success("正在跳转到所有共享的日程表页面");
            window.location.href="/shareCommunity/showShareSframe";
        });

        //加载页面信息
        $.post({
            url:"/shareCommunity/loadShareCommunity",
            success:function (json) {

                var sharedScheduleForms =json.scheduleForms;
                var sharedScheduleFrames=json.scheduleFrames;
                //用于sform拼接
                var form="";
                //用于sframe拼接
                var frame="";
                var sformStatus="checked";
                var sframeStatus="checked";
                var publish='off';
                for (var i=0;i<sharedScheduleForms.length;i++) {

                    if (i>=3) {
                        break;
                    }
                    var creatTime=createTime(sharedScheduleForms[i].creatTime);

                    if (sharedScheduleForms[i].isShare!="on"){
                        sformStatus="";

                    }
                    form+="<div class=\"layui-col-xs12 layui-col-sm6 layui-col-md3 \" id=\"show-btn\">\n" +
                        "    <div class=\"layui-card layui-height-lwh\" id=\"show1\">\n" +
                        "        <div class=\"box layui-card-background-lwho\">\n" +
                        "            <div class=\"box-content\">\n" +
                        "                <div class=\"layui-card-header layui-height-lwh-header\">\n" +"" +
                        "                <input type=\"text\" id='selectId' value='"+sharedScheduleForms[i].sformId+"' hidden />"+
                        "                    <label>创建时间："+creatTime+"</label>\n" +
                        "                </div>\n" +
                        "                <div class=\"layui-card-body layui-height-lwh-body\">\n" +
                        "                    <p class=\"lay-big-font\">"+sharedScheduleForms[i].sformName+"</p>\n" +
                        "                    <p><span class=\"pull-right\">\n" +
                        "                                                  <input type=\"checkbox\" name=\"my_schedule_open_1\" lay-filter=\"myfirst_share\"\n" +
                        "                                                         id='"+sharedScheduleForms[i].sformId+" ' lay-skin='switch' lay-text=\"公开|私有\" "+sformStatus+" ></span></p>\n" +
                        "                </div>\n" +
                        "                <ul   class=\"icon\">\n" +
                        "                    <li >\n" +
                        "                        <a  lay-filter='updateBtn' href='javaScript:openRevise("+sharedScheduleForms[i].sformId+")' > <i class=\"fa fa-magic\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a href='/myScheduleForm/checkScheduleForm?sformId="+sharedScheduleForms[i].sformId+"'  id='check'><i class=\"fa fa-check\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a  id='combine' href='javaScript:combineSform("+sharedScheduleForms[i].sformId+")' ><i class=\"fa fa-legal\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a  id='remove'href='javaScript:isDelete("+sharedScheduleForms[i].sformId+")' ><i class=\"fa fa-remove\"></i></a>\n" +
                        "                    </li>\n" +
                        "                </ul>\n" +
                        "            </div>\n" +
                        "        </div>\n" +
                        "    </div>\n" +
                        "</div>"

                }



                for (var i=0;i<sharedScheduleFrames.length;i++) {
                    if (i>=3) {
                        break;
                    }
                    // alert(draftScheduleFrames[i].publish);

                    if (sharedScheduleFrames[i].publish!="on"){
                        sframeStatus="";
                        publish='true';
                    }

                    frame+="<div class=\"layui-col-xs12 layui-col-sm6 layui-col-md3 \" id=\"show-btn\">\n" +
                        "    <div class=\"layui-card layui-height-lwh\" id=\"show1\">\n" +
                        "        <div class=\"box layui-card-background-lwho\">\n" +
                        "            <div class=\"box-content\">\n" +
                        "                <div class=\"layui-card-header layui-height-lwh-header\">\n" +
                        "                    <label>创建时间："+sharedScheduleFrames[i].creatTime+"</label>\n" +
                        "                </div>\n" +
                        "                <div class=\"layui-card-body layui-height-lwh-body\">\n" +
                        "                    <p class=\"lay-big-font\">"+sharedScheduleFrames[i].sframeName+"</p>\n" +
                        "                    <p><span class=\"pull-right\">\n" +
                        "                                                  <input type=\"checkbox\" name=\"my_schedule_open_1\" lay-filter=\"myfirst_share\"\n" +
                        "                                                         id='"+sharedScheduleFrames[i].sframeId+" ' lay-skin='switch' lay-text=\"公开|私有\" "+sframeStatus+"  ></span></p>\n" +
                        "                </div>\n" +
                        "                <ul class=\"icon\">\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:copySframe("+sharedScheduleFrames[i].sframeId+")' ><i class=\"fa fa-copy\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:reviseSframe("+sharedScheduleFrames[i].sframeId+","+publish+")' ><i class=\"fa fa-magic\"></i></a>\n" +
                        "                    </li>\n" +
                        "                    <li>\n" +
                        "                        <a href='javaScript:deleteSframe("+sharedScheduleFrames[i].sframeId+")' ><i class=\"fa fa-remove\"></i></a>\n" +
                        "                    </li>\n" +
                        "                </ul>\n" +
                        "            </div>\n" +
                        "        </div>\n" +
                        "    </div>\n" +
                        "</div>"


                }


                $("#showsfrom").html(form);
                $("#showsframe").html(frame);
                publish='off';
                status='checked';

                layui.use('form', function() {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
            }
        });




        /*        //修改日程表
                $('#updateBtn').click(function () {
                    alert("dfhafd");
                    layer.open({
                        type:2,
                        area: ['500px', '300px'],
                        title: '修改日程',
                        content: Feng.ctxPath + '/myScheduleForm/reviseSform',
                        tableId: MgrUser.tableId
                    });
                });*/





        /*
                //删除
                $('#remove').on('click', function(){
                    var othis = $(this);
                    alert(othis);
                    alert("graeh")
                    layer.confirm('您是确定要删除该日程表吗？', {
                            btn: ['确定','取消'] //按钮
                        },
                        function(){
                            layer.msg('确定', {icon: 1});
                            $.post({
                                url:"/myScheduleForm/deleteSformBySformId?sformId="+$("#selectId").valueOf(),
                                success:function (data) {
                                    console.log(data);
                                    if (data.valueOf()=='删除日程表成功！') {
                                                                   Feng.success(data.valueOf()+"正跳转到日程表页面");
                                                                 /!*   /!*!//关闭当前窗口*!/
                                                                    admin.closeThisDialog();
                                        parent.location.href="/myScheduleForm/showScheduleForm";

                                    }else {
                                        Feng.error(data.valueOf());
                                    }
                                }
                            });

                        });
                });

        */




    });


    $('#cancel,#cancel2,#cancel3,#cancel4').on('click', function(){
        var othis = $(this);
        layer.open({
            content: '您确认取消认购？',
            btn: ['确认', '取消'],
            shadeClose: false,
            yes: function(){
                layer.open({content: '确认取消认购', time: 1});
                othis.parents('.atxt').remove();
            }, no: function(){
                layer.open({content: '您选择了取消', time: 1});
            }
        });
    });


    form.on('#remove',function () {
        var othis = $(this);
        // alert(othis);
        // alert("graeh")
        layer.confirm('您是确定要删除该日程表吗？', {
                btn: ['确定','取消'] //按钮
            },
            function(){
                layer.msg('确定', {icon: 1});
                $.post({
                    url:"/myScheduleForm/deleteSformBySformId?sformId="+$("#selectId").valueOf(),
                    success:function (data) {
                        console.log(data);
                        if (data.valueOf()=='删除日程表成功！') {
                            Feng.success(data.valueOf()+"正跳转到日程表页面");
                            /*   /!*!//关闭当前窗口*/
                            admin.closeThisDialog();
                            parent.location.href="/myScheduleForm/showScheduleForm";

                        }else {
                            Feng.error(data.valueOf());
                        }
                    }
                });

            });

    });

    form.on('switch(myfirst_share)', function(data){
        alert("ASgG")
        if (data.elem.checked){
            layer.msg(data.elem.id,{time:5000});
            alert(data.value)
        };
        var sformId=data.elem.id;
        var status=data.elem.checked;
        $.ajax({
            url:"/myScheduleForm/setShare?sformId="+sformId+"&status="+status,
            type:"get",
            dataType:"json",
            success:function(data){
                console.log(data);
                layer.msg(data.msg);
            },
            error:function(data){
                $.messager.alert('错误',data.msg);
            }
        });
        // layer.close(index);
    });


});
